<template>
  <div class="customer-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="collapsed ? '64px' : '200px'" class="sidebar">
        <div class="sidebar-header">
          <div class="logo" v-if="!collapsed">
            <el-icon size="28" color="#2196F3">
              <User />
            </el-icon>
            <span class="logo-text">客户中心</span>
          </div>
          <el-icon v-else size="28" color="#2196F3">
            <User />
          </el-icon>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          :collapse="collapsed"
          :unique-opened="true"
          router
          class="sidebar-menu"
        >
          <el-menu-item index="/customer/dashboard">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-menu-item index="/customer/browse">
            <el-icon><Search /></el-icon>
            <span>物品浏览</span>
          </el-menu-item>
          
          <el-menu-item index="/customer/create-order">
            <el-icon><Plus /></el-icon>
            <span>创建订单</span>
          </el-menu-item>
          
          <el-menu-item index="/customer/orders">
            <el-icon><Document /></el-icon>
            <span>我的订单</span>
          </el-menu-item>
          
          <el-menu-item index="/customer/history">
            <el-icon><Clock /></el-icon>
            <span>历史记录</span>
          </el-menu-item>
          
          <el-menu-item index="/customer/profile">
            <el-icon><Setting /></el-icon>
            <span>个人信息</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="header">
          <div class="header-left">
            <el-button
              text
              @click="toggleSidebar"
              class="collapse-btn"
            >
              <el-icon size="20">
                <Fold v-if="!collapsed" />
                <Expand v-else />
              </el-icon>
            </el-button>
            
            <el-breadcrumb separator="/" class="breadcrumb">
              <el-breadcrumb-item :to="{ path: '/customer' }">客户中心</el-breadcrumb-item>
              <el-breadcrumb-item>{{ currentPageTitle }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          
          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <div class="user-info">
                <el-avatar :size="32" :src="userAvatar">
                  {{ userName.charAt(0) }}
                </el-avatar>
                <span class="username">{{ userName }}</span>
                <el-icon><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                  <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>

        <!-- 主内容 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessageBox } from 'element-plus'
import {
  User,
  HomeFilled,
  Search,
  Plus,
  Document,
  Clock,
  Setting,
  Fold,
  Expand,
  ArrowDown
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const store = useStore()

const collapsed = ref(false)
const userAvatar = ref('')

const activeMenu = computed(() => route.path)
const userName = computed(() => store.getters.userName)

const currentPageTitle = computed(() => {
  const routeMap = {
    '/customer/dashboard': '首页',
    '/customer/browse': '物品浏览',
    '/customer/create-order': '创建订单',
    '/customer/orders': '我的订单',
    '/customer/history': '历史记录',
    '/customer/profile': '个人信息'
  }
  return routeMap[route.path] || '首页'
})

const toggleSidebar = () => {
  collapsed.value = !collapsed.value
}

const handleCommand = async (command) => {
  switch (command) {
    case 'profile':
      router.push('/customer/profile')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        store.dispatch('logout')
        router.push('/login')
      } catch {
        // 用户取消
      }
      break
  }
}
</script>

<style scoped>
.customer-layout {
  height: 100vh;
}

.sidebar {
  background: #fff;
  border-right: 1px solid #e6e6e6;
  transition: width 0.3s;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #e6e6e6;
  padding: 0 16px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-text {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.sidebar-menu {
  border: none;
  height: calc(100vh - 60px);
}

.header {
  background: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.collapse-btn {
  padding: 8px;
}

.breadcrumb {
  font-size: 18px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.username {
  font-size: 16px;
  color: #333;
}

.main-content {
  background: #f5f5f5;
  padding: 24px;
  overflow-y: auto;
}
</style>
